import { defineStore } from 'pinia';
import {ElMessage, ElMessageBox} from "element-plus";
import router from "../router";


export const useUserStore = defineStore('user_data', {
    state: () => ({
        user: JSON.parse(localStorage.getItem('getUser')as string)|| null,
       
        isLogin:localStorage.getItem('isLogin')||false
    }),
    getters: {
        getUser: (state) => state.user,
        isAuthenticated: (state) => state.isLogin,
    },
    actions: {
        setUser(uname:string,psw:string,) {
            if (uname.trim()===''||psw.trim()===''){
                ElMessage({
                    type: 'info',
                    message: '输入不能为空',
                })
                return;
            }

            let obj={uname:uname,psw:psw,}
            this.user = obj;
            localStorage.setItem('getUser',JSON.stringify(this.user))
            ElMessage({
                type: 'success',
                message: '注册成功',
            })
            router.replace({path:'/login'})
        },
        login(uname:string,psw:string) {
            // let user = JSON.parse(localStorage.getItem('getUser'));
            if (uname.trim()===''||psw.trim()===''){
                ElMessage({
                    type: 'info',
                    message: '输入不能为空',
                })
                return;
            }
            if(this.getUser==null){
                ElMessage({
                    type: 'info',
                    message: '用户未注册',
                })
                return;
            }
            if (uname==this.getUser.uname){
             
                if (psw==this.getUser.psw){
                    ElMessage({
                        type: 'success',
                        message: '登录成功',
                    })
                    this.isLogin=true
                    localStorage.setItem('islogin',JSON.stringify(this.isLogin))
                }else {
                    ElMessage({
                        type: 'info',
                        message: '密码错误',
                    })
                    return;
                }
            }else {
                ElMessage({
                    type: 'info',
                    message: '用户名错误',
                })
                return;
            }
            router.replace({path:'/gindex'})
        },

        //1
        getUname() {
            return this.user ? this.user.uname : null;
        },
        //1
        logout() {

            ElMessageBox.confirm(
                '你确定要退出登录吗?',
                '提示',
                {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                }
            )
                .then(() => {
                    // this.user = null;
                    // localStorage.removeItem('getUser')
                   
                    ElMessage({
                        type: 'success',
                        message: '退出登录成功',
                    })
                    router.replace({path:'/login'})
                    this.isLogin=false
                    localStorage.setItem('islogin',JSON.stringify(this.isLogin))
                })
                .catch(() => {
                    ElMessage({
                        type: 'info',
                        message: '退出登录取消',
                    })
                })
        },
    },
});
